
local console_type = '小型控制台'
local japi = require 'jass.japi'

-- 控制台 -- 
local console = {

    unitdetail = ac.frame.unitdetail:add_panel(''),

    herodetail = ac.frame.iconhero:add_panel(''),

    --- 注册全局事件
    event = function(self,name)
        return ac.register(self, name)
    end,
    
    --- 派发全局事件(有返回值)
    dispatch = function(self,name, ...)
        return ac.dispatch(self, name, ...)
    end,
    
    --- 派发全局事件
    notify = function(self,name, ...)
        return ac.notify(self, name, ...)
    end,
}

ac.console = console 

console.minimap = ac.frame.convert(japi.FrameGetMinimap(), true, nil, true)


function console.minimap:set_position(x,y)
    self.x = x
    self.y = y
    local ax =  x / 1920 * 0.8
    local ay = (1080 - y - self.h) / 1080 * 0.6
    local bx = (x + self.w) / 1920 * 0.8
    local by = (1080 - y) / 1080 * 0.6
    japi.FrameSetPoint(self.handle,6,japi.GetGameUI(),6,ax,ay)
    japi.FrameSetPoint(self.handle,2,japi.GetGameUI(),6,bx,by)
end

console.minimapbutton = {
    class.button.convert(japi.FrameGetMinimapButton(0), true, nil, true),
    class.button.convert(japi.FrameGetMinimapButton(1), true, nil, true),
    class.button.convert(japi.FrameGetMinimapButton(2), true, nil, true),
    class.button.convert(japi.FrameGetMinimapButton(3), true, nil, true),
    class.button.convert(japi.FrameGetMinimapButton(4), true, nil, true),
}


console.item = {
    [0x01] = class.button.convert( japi.FrameGetItemBarButton(0), true, nil, true),
    [0x02] = class.button.convert( japi.FrameGetItemBarButton(1), true, nil, true),
    [0x03] = class.button.convert( japi.FrameGetItemBarButton(2), true, nil, true),
    [0x04] = class.button.convert( japi.FrameGetItemBarButton(3), true, nil, true),
    [0x05] = class.button.convert( japi.FrameGetItemBarButton(4), true, nil, true),
    [0x06] = class.button.convert( japi.FrameGetItemBarButton(5), true, nil, true),
}

console.ability = {
    [0x01] = class.button.convert(  japi.FrameGetCommandBarButton(0,0), true, nil, true ),
    [0x02] = class.button.convert(  japi.FrameGetCommandBarButton(0,1), true, nil, true ),
    [0x03] = class.button.convert(  japi.FrameGetCommandBarButton(0,2), true, nil, true ),
    [0x04] = class.button.convert(  japi.FrameGetCommandBarButton(0,3), true, nil, true ),
    [0x05] = class.button.convert(  japi.FrameGetCommandBarButton(1,0), true, nil, true ),
    [0x06] = class.button.convert(  japi.FrameGetCommandBarButton(1,1), true, nil, true ),
    [0x07] = class.button.convert(  japi.FrameGetCommandBarButton(1,2), true, nil, true ),
    [0x08] = class.button.convert(  japi.FrameGetCommandBarButton(1,3), true, nil, true ),
    [0x09] = class.button.convert(  japi.FrameGetCommandBarButton(2,0), true, nil, true ),
    [0x0A] = class.button.convert(  japi.FrameGetCommandBarButton(2,1), true, nil, true ),
    [0x0B] = class.button.convert(  japi.FrameGetCommandBarButton(2,2), true, nil, true ),
    [0x0C] = class.button.convert(  japi.FrameGetCommandBarButton(2,3), true, nil, true ),
}

console.abilitydetail = {
    console.unitdetail:add_panel('', 1483.5, 0844.3, 91.3, 68.4 ),
    console.unitdetail:add_panel('', 1588.1, 0844.3, 91.3, 68.4 ),
    console.unitdetail:add_panel('', 1692.9, 0844.3, 91.3, 68.4 ),
    console.unitdetail:add_panel('', 1797.6, 0844.3, 91.3, 68.4 ),
    console.unitdetail:add_panel('', 1483.5, 0921.6, 91.3, 68.4 ),
    console.unitdetail:add_panel('', 1588.1, 0921.6, 91.3, 68.4 ),
    console.unitdetail:add_panel('', 1692.9, 0921.6, 91.3, 68.4 ),
    console.unitdetail:add_panel('', 1797.6, 0921.6, 91.3, 68.4 ),
    console.unitdetail:add_panel('', 1483.5, 0999.0, 91.3, 68.4 ),
    console.unitdetail:add_panel('', 1588.1, 0999.0, 91.3, 68.4 ),
    console.unitdetail:add_panel('', 1692.9, 0999.0, 91.3, 68.4 ),
    console.unitdetail:add_panel('', 1797.6, 0999.0, 91.3, 68.4 ),
}

console.itemdetail = {
    console.unitdetail:add_panel('', 1239, 0879, 75, 56 ),
    console.unitdetail:add_panel('', 1335, 0879, 75, 56 ),
    console.unitdetail:add_panel('', 1239, 0947.5, 75, 56 ),
    console.unitdetail:add_panel('', 1335, 0947.5, 75, 56 ),
    console.unitdetail:add_panel('', 1239, 1016, 75, 56 ),
    console.unitdetail:add_panel('', 1335, 1016, 75, 56 ),
}

--技能按钮
for i,panel in ipairs(console.abilitydetail) do
    panel.slotid = i
end

--物品按钮
for i,panel in ipairs(console.itemdetail) do
    panel.slotid = i
end

local function init()

    for i, button in ipairs(console.item) do 
        button.slotid = i
        button.command = 'item'
        button.on_button_mouse_enter = function()
            console.focus = button
            console:notify("控制台-指向物品按钮", button, ac.unit.localunit())
        end 
        button.on_button_mouse_leave = function()
            console:notify("控制台-离开物品按钮", button, ac.unit.localunit())
            console.focus = nil 
        end 
    end
    
    for i, button in ipairs(console.ability) do 
        button.slotid = i
        button.command = 'ability'
        button.column = (i-1) % 4
        button.row = (i-1) // 4 
        
        button.on_button_mouse_enter = function()
            console.focus = button
            console:notify("控制台-指向技能按钮", button, ac.unit.localunit())
        end 
        button.on_button_mouse_leave = function()
            console:notify("控制台-离开技能按钮", button, ac.unit.localunit())
            console.focus = nil 
        end 
    end
end 

init()

function console:refresh(unit)
    if not unit then return end
    if not self.focus then return end 
    local focus = self.focus
    local command = focus.command
    if command == 'ability' then 
        console:notify("控制台-离开技能按钮", focus, unit)
        console:notify("控制台-指向技能按钮", focus, unit)
    elseif command == 'item' then 
        console:notify("控制台-离开物品按钮", focus, unit)
        console:notify("控制台-指向物品按钮", focus, unit)
    elseif command == 'buff' then 
        console:notify("控制台-离开状态按钮", focus, unit)
        console:notify("控制台-指向状态按钮", focus, unit)
    elseif command == 'attack' then 
        console:notify("控制台-离开攻击按钮", focus, unit)
        console:notify("控制台-指向攻击按钮", focus, unit)
    elseif command == 'amor' then 
        console:notify("控制台-离开护甲按钮", focus, unit)
        console:notify("控制台-指向护甲按钮", focus, unit)
    elseif command == 'progress' then 
        console:notify("控制台-离开经验条", focus, unit)
        console:notify("控制台-指向经验条", focus, unit)
    end 
end

ac.message:event "选择单位" ( function (handle)
    local unit = ac.unit(handle) 
    if unit then 
        console:refresh(unit)
    end 
end )

-- ac.hardware:on "鼠标点击" (function(t)
--     local unit = ac.unit:localunit() 
--     local button = console.focus
--     if unit and button then
--         if unit.play_skills and button.command == 'ability' then 
--             local x = button.slotx
--             local y = button.sloty
--             local abilityid = ac.message.button(x, y)
--             if abilityid then 
--                 local skill = unit.play_skills[abilityid]
--                 if skill and skill:is_passive() then 
--                     return true  
--                 end 
--             end 
--         -- elseif unit.items and button.command == 'item' then 
--         --     local slotid = button.slotid
--         --     local item = unit.items[slotid]
--         --     if item and not item:can_use() then 
--         --         return true  
--         --     end 
--         end
--     end 
-- end)

ac.hardware:on "界面绘制" (function(t)
    console:notify("控制台-刷新")
end)

return console